// Copyright (c) 2023, json_wang and contributors
// For license information, please see license.txt

frappe.ui.form.on('ServiceOrder', {
	refresh: function (frm) {
		frm.add_custom_button('自定义打印', () => {
			console.log(frm)
			var printJson = JSON.stringify(frm.doc);
			window.chrome.webview.postMessage(printJson);
		})
	},
	onload(frm){
		//明细行级联选择
		frm.set_query("service_type","lines", function(doc, cdt, cdn) {
			const row = doc.lines.find(f=>f.name === cdn)			     
			return {
				"filters": {
					"service_type_group": row.service_type_group				
				}
			};
		});
	}

});


frappe.ui.form.on('ServiceOrderLine', { // The child table is defined in a DoctType called "Dynamic Link"
    work_time(frm, cdt, cdn) { // "links" is the name of the table field in ToDo, "_add" is the event
        // frm: current ToDo form
        // cdt: child DocType 'Dynamic Link'
        // cdn: child docname (something like 'a6dfk76')
        // cdt and cdn are useful for identifying which row triggered this event
		var total = 0;
		frm.doc.lines.forEach(element => {
			if (element.work_time)
			{
				total += element.work_time;
			}			
		});
		frm.set_value('total_time', total);
        console.log(frm);
    },
	service_type_group(frm, cdt, cdn){
		//console.log(frm)
		let row = frm.doc.lines.find(f=>f.name === cdn)
		row.service_type = null
		frm.refresh_field('lines')
	}
});
